package com.fc.redap.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.fc.redap.controller.BaseController;
import com.fc.redap.dtos.ResponseJson;
import com.fc.redap.entity.ProjectBookedUserEntity;
import com.fc.redap.entity.ProjectEntity;
import com.fc.redap.entity.ProjectIntroImageEntity;
import com.fc.redap.entity.RegionEntity;
import com.fc.redap.helper.DateTimeHelper;
import com.fc.redap.service.IProjectBookedUserService;
import com.fc.redap.service.IProjectIntroImageService;
import com.fc.redap.service.IProjectService;
import com.fc.redap.service.IRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/project")
@Api(value="旅游项目相关的API", tags={"旅游项目"})
public class ProjectController extends BaseController {

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IProjectIntroImageService projectIntroImageService;

    @Autowired
    private IProjectBookedUserService projectBookedUserService;

    @ApiOperation(value = "POST -- 获取项目列表",
            notes = " REST 方式请求。"+
                    "Request Parameter: <ul>" +
                    "<li><i>可选参数: projectName: 项目名称 模糊查询</i></li>" +
                    "<li><i>可选参数: count: 每页记录数</i></li>" +
                    "<li><i>可选参数: page: 第几页</i></li>" +
                    "</ul>",
            response = ResponseJson.class,
            httpMethod = "POST",
            consumes = "application/json",
            produces = "application/json",
            code = 200
    )
    @PostMapping("/list")
    public ResponseJson doQueryList(@RequestBody JSONObject params) {

        params.put("limit",params.get("count"));
        params.put("offset",params.get("page"));
        //校对并赋值 offset和limit
        calculatePagination(params);
        params.put("startOfToday", DateTimeHelper.getStartOfDay(DateTimeHelper.getCurrentDay()));

        List<ProjectEntity> projectList = projectService.queryList(params);
        Map<String, Object> bookReqMap = new HashMap<>();
        for(ProjectEntity p : projectList){
            bookReqMap.clear();
            bookReqMap.put("projectId",p.getId());
            List<ProjectBookedUserEntity> userList = projectBookedUserService.queryBookeUserList(bookReqMap);
            if(userList !=null){
                p.setBookedNumber(userList.size());
            }else{
                p.setBookedNumber(0);
            }
        }

        return ResponseJson.success(projectList);
    }

    @ApiOperation(value = "POST -- 保存预约红色旅游项目",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>必填参数：projectName: 项目名称</i></li>" +
                    "<li><i>必填参数：displayImgUrl: 首页展示照片</i></li>" +
                    "<li><i>可选参数：projectStartTime: Date,项目开始时间</i></li>" +
                    "<li><i>可选参数：projectEndTime: Date,项目结束时间</i></li>" +
                    "<li><i>可选参数：limitNumber: Integer,项目限制的人数 如果为0则不限制人数</i></li>" +
                    "<li><i>可选参数：showLimitNumber: Integer,是否显示人数 0-不显示  1-显示</i></li>" +
                    "<li><i>可选参数：projectDesc: String,项目描述</i></li>" +
                    "<li><i>可选参数：status: Integer,项目状态 1- 草稿 2 -发布</i></li>" +
                    "<li><i>可选参数：publishTime: Date,发布时间</i></li>" +
                    "<li><i>可选参数：bookStartTime: Date,接受预定开始时间</i></li>" +
                    "<li><i>可选参数：bookEndTime: Date,接受预定结束时间</i></li>" +
                    "<li><i>可选参数：bookedNumber: Integer,已预定人数</i></li>" +
                    "<li><i>可选参数：introImages: List,旅游项目介绍的图片信息列表</i></li>" +
                    "<li><i>可选参数：scenicZones: List,旅游项目所涉及的地点信息列表</i></li>" +
                    "<li><i>可选参数：scenicZones: List,旅游项目所涉及的地点信息列表</i></li>" +
                    "</ul>",
            response = ResponseJson.class,
            httpMethod = "POST",
            consumes = "application/json",
            produces = "application/json",
            code = 200
    )
    @PostMapping("/save")
    public ResponseJson doSave(@RequestBody ProjectEntity projectEntity) {

//        projectEntity.setCreateUserId(getUserId());
        if(projectEntity.getId() == null || projectEntity.getId() == 0l){
            projectEntity.setLimitNumber(999);
            projectEntity.setBookedNumber(0);
            projectEntity.setStatus(2);
            projectService.save(projectEntity);
        }else{
            ProjectEntity project = projectService.queryObject(projectEntity.getId());
            if(project == null){
                projectService.save(projectEntity);
            }else{
                projectService.update(projectEntity);
            }
        }

        return ResponseJson.success();
    }

    @ApiOperation(value = "PUT -- 保存预约红色旅游项目",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>必填参数：projectName: 项目名称</i></li>" +
                    "<li><i>必填参数：displayImgUrl: 首页展示照片</i></li>" +
                    "<li><i>可选参数：projectStartTime: Date,项目开始时间</i></li>" +
                    "<li><i>可选参数：projectEndTime: Date,项目结束时间</i></li>" +
                    "<li><i>可选参数：limitNumber: Integer,项目限制的人数 如果为0则不限制人数</i></li>" +
                    "<li><i>可选参数：showLimitNumber: Integer,是否显示人数 0-不显示  1-显示</i></li>" +
                    "<li><i>可选参数：projectDesc: String,项目描述</i></li>" +
                    "<li><i>可选参数：status: Integer,项目状态 1- 草稿 2 -发布</i></li>" +
                    "<li><i>可选参数：publishTime: Date,发布时间</i></li>" +
                    "<li><i>可选参数：bookStartTime: Date,接受预定开始时间</i></li>" +
                    "<li><i>可选参数：bookEndTime: Date,接受预定结束时间</i></li>" +
                    "<li><i>可选参数：bookedNumber: Integer,已预定人数</i></li>" +
                    "<li><i>可选参数：introImages: List,旅游项目介绍的图片信息列表</i></li>" +
                    "<li><i>可选参数：scenicZones: List,旅游项目所涉及的地点信息列表</i></li>" +
                    "</ul>",
            response = ResponseJson.class,
            httpMethod = "PUT",
            consumes = "application/json",
            produces = "application/json",
            code = 200
    )
    @PutMapping("/update")
    public ResponseJson doUpdate(@RequestBody ProjectEntity projectEntity) {

//        projectEntity.setCreateUserId(getUserId());
        projectService.update(projectEntity);
        return ResponseJson.success();
    }

    @ApiOperation(value = "GET 获取指定旅游项目的详细信息",
            notes = "获取指定旅游项目的详细信息",
            response = ResponseJson.class,
            httpMethod = "GET",
            consumes = "application/json",
            produces = "application/json"
    )
    @RequestMapping("/info/{projectId}")
    public ResponseJson info(@PathParam("projectId") @PathVariable("projectId") Long projectId)
    {
        ProjectEntity projectEntity = projectService.getProjectInfoById(projectId);
        Map<String, Object> bookReqMap = new HashMap<>();
        bookReqMap.put("projectId",projectEntity.getId());
        List<ProjectBookedUserEntity> userList = projectBookedUserService.queryBookeUserList(bookReqMap);
        if(userList !=null){
            projectEntity.setBookedNumber(userList.size());
        }else{
            projectEntity.setBookedNumber(0);
        }
        return ResponseJson.success(projectEntity);
    }

    @DeleteMapping("/delete/{projectId}")
    public ResponseJson deleteProject(@PathParam("projectId") @PathVariable("projectId") Long projectId){
        projectService.delete(projectId);
        return ResponseJson.success();
    }

    @DeleteMapping("/introimage")
    public ResponseJson deleteImage(@RequestBody ProjectIntroImageEntity imageEntity) {
        projectIntroImageService.delete(imageEntity.getId());
        return ResponseJson.success();
    }

    @PutMapping("/introimage/{imageId}")
    public ResponseJson deleteImageById(@PathParam("imageId") @PathVariable("imageId") Long imageId) {
        projectIntroImageService.delete(imageId);
        return ResponseJson.success();
    }
}
